function [NewW1,NewB1,NewW2,NewB2]=PSO_GATrain(SamIn,SamOut,HiddenUnitNum);
Maxgeneration=30000;
E0=0.001;
Vmin=-120;
Vmax=120;
M=84;
c1=2;
c2=2;
w=0.9;
pcr=0.5;
pmu=0.04;
[R,SamNum]=size(SamIn);
[S2,SamNum]=size(SamOut);
generation=1;
Done=0;
Pb1=zeros(HiddenUnitNum,R,M);
Pb2=zeros(HiddenUnitNum,1,M);
Pb3=zeros(S2,1,M);
Pb4=zeros(S2,HiddenUnitNum,M);
Pg1=zeros(HiddenUnitNum,R);
Pg2=zeros(HiddenUnitNum,1);
Pg3=zeros(S2,1);
Pg4=zeros(S2,HiddenUnitNum);
E=zeros(size(SamOut));
rand('state',sum(100*clock));
startP1=rand(HiddenUnitNum,R,M)-0.5;
startP2=rand(HiddenUnitNum,1,M)-0.5;
startP3=rand(S2,1,M)-0.5;
startP4=rand(S2,HiddenUnitNum,M)-0.5;
startV1=rand(HiddenUnitNum,R,M)-0.5;
startV2=rand(HiddenUnitNum,1,M)-0.5;
startV3=rand(S2,1,M)-0.5;
startV4=rand(S2,HiddenUnitNum,M)-0.5;
endP1=zeros(HiddenUnitNum,R,M);
endP2=zeros(HiddenUnitNum,1,M);
endP3=zeros(S2,1,M);
endP4=zeros(S2,HiddenUnitNum,M);
endV1=zeros(HiddenUnitNum,R,M);
endV2=zeros(HiddenUnitNum,1,M);
endV3=zeros(S2,1,M);
endV4=zeros(S2,HiddenUnitNum,M);

startE=zeros(1,M);
endE=zeros(1,M);
LP1=pcr;
LP2=pmu;
NetworkOut=zeros(size(SamOut));
for i=1:M
    W1=startP1(1:HiddenUnitNum,1:R,i);
    B1=startP2(1:HiddenUnitNum,1,i);
    B2=startP3(1:S2,1,i);
    W2=startP4(1:S2,1:HiddenUnitNum,i);
    for q=1:SamNum
        TempOut=logsig(W1*SamIn(:,q)+B1);
        NetworkOut(:,q)=W2*TempOut+B2;
    end
    E=NetworkOut-SamOut;
    startE(1,i)=sumsqr(E)/(SamNum*S2);
    Pb1(:,:,i)=startP1(:,:,i);
    Pb2(:,:,i)=startP2(:,:,i);
    Pb3(:,:,i)=startP3(:,:,i);
    Pb4(:,:,i)=startP4(:,:,i);
end
[val,position]=min(startE(1,:));
Pg1=startP1(:,:,position);
Pg2=startP2(:,:,position);
Pg3=startP3(:,:,position);
Pg4=startP4(:,:,position);
Pgvalue=val;
ErrHistory=[];
while(~Done)
    for num=1:M
        endV1(:,:,num)=w*startV1(:,:,num)+c1*rand*(Pb1(:,:,num)-startP1(:,:,num))+c2*rand*(Pg1-startP1(:,:,num));
        endV2(:,:,num)=w*startV2(:,:,num)+c1*rand*(Pb2(:,:,num)-startP2(:,:,num))+c2*rand*(Pg2-startP2(:,:,num));
        endV3(:,:,num)=w*startV3(:,:,num)+c1*rand*(Pb3(:,:,num)-startP3(:,:,num))+c2*rand*(Pg3-startP3(:,:,num));
        endV4(:,:,num)=w*startV4(:,:,num)+c1*rand*(Pb4(:,:,num)-startP4(:,:,num))+c2*rand*(Pg4-startP4(:,:,num));

        for i=1:HiddenUnitNum
            for j=1:R
                if endV1(i,j,num)>Vmax
                    endV1(i,j,num)=Vmax;
                elseif endV1(i,j,num)<Vmin
                        endV1(i,j,num)=Vmin;
                end
            end
        end
        for i=1:HiddenUnitNum
            if endV2(i,1,num)>Vmax
               endV2(i,1,num)=Vmax;
            elseif endV2(i,1,num)<Vmin
                   endV2(i,1,num)=Vmin;
            end
        end
        for i=1:S2
            if endV3(i,1,num)>Vmax
                endV3(i,1,num)=Vmax;
            elseif endV3(i,1,num)<Vmin
                endV3(i,1,num)=Vmin;
            end
        end
        for i=1:S2
            for j=1:HiddenUnitNum
                if endV4(i,j,num)>Vmax
                    endV4(i,j,num)=Vmax;
                elseif endV4(i,j,num)<Vmin
                        endV4(i,j,num)=Vmin;
                end
            end
        end
        endP1(:,:,num)=startP1(:,:,num)+endV1(:,:,num);
        endP2(:,:,num)=startP2(:,:,num)+endV2(:,:,num);
        endP3(:,:,num)=startP3(:,:,num)+endV3(:,:,num);
        endP4(:,:,num)=startP4(:,:,num)+endV4(:,:,num);

        W1=endP1(1:HiddenUnitNum,1:R,num);
        W2=endP4(1:S2,1:HiddenUnitNum,num);
        B2=endP3(1:S2,1,num);
        B1=endP2(1:HiddenUnitNum,1,num);
        for q=1:SamNum
            TempOut=logsig(W1*SamIn(:,q)+B1);
            NetworkOut(:,q)=W2*TempOut+B2;
        end
        E=NetworkOut-SamOut;
        SSE=sumsqr(E);  
        ErrHistory=[ErrHistory SSE];
        endE(1,num)=SSE/(SamNum*S2);
        if endE(1,num)<startE(1,num)
            Pb1(:,:,num)=endP1(:,:,num);
            Pb2(:,:,num)=endP2(:,:,num);
            Pb3(:,:,num)=endP3(:,:,num);
            Pb4(:,:,num)=endP4(:,:,num);
            startE(1,num)=endE(1,num);
        end
        startP1(:,:,num)=endP1(:,:,num);
        startP2(:,:,num)=endP2(:,:,num);
        startP3(:,:,num)=endP3(:,:,num);
        startP4(:,:,num)=endP4(:,:,num);
        startV1(:,:,num)=endV1(:,:,num);
        startV2(:,:,num)=endV2(:,:,num);
        startV3(:,:,num)=endV3(:,:,num);
        startV4(:,:,num)=endV4(:,:,num);
    end
    w=0.9-(0.5/Maxgeneration)*generation;
    [value,position]=min(startE(1,:));
    if value<Pgvalue
        Pg1=Pb1(:,:,position);
        Pg2=Pb2(:,:,position);
        Pg3=Pb3(:,:,position);
        Pg4=Pb4(:,:,position);
        Pgvalue=value;
    end
    if (generation>=Maxgeneration)
        Done=1;
    end
    if Pgvalue<E0
        Done=1;
    end
    generation=generation+1;
end

W1=Pg1(1:HiddenUnitNum,1:R);
W2=Pg4(1:S2,1:HiddenUnitNum);
B2=Pg3(1:S2,1);
B1=Pg2(1:HiddenUnitNum,1);
NewW1=W1;
NewW2=W2;
NewB1=B1;
NewB2=B2;
